<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Finance extends Controller
{
    public function index()
    {
        /*set_time_limit(0);
        $ids = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('pay_uniacid',334)->value('group_concat(giftlog_id)');
        
        $list = Db::connect("live_db_config")->table('ims_wzbagent_giftlogs')->where('pay_uniacid','334')->where('is_gift','1')->where('dateline','<=',1686758400)->where('dateline','>=',1686672000)->order('id asc')->limit($_GET['page']*50,50)->select();//->select();->whereNotIn('id',$ids)
        echo count($list);
        if(count($list) == 0)
            exit;
        foreach ($list as $k=>$v)
        {
            if(! Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('transaction_id',$v['transaction_id'])->count())
            {
                echo $v['id'];
                Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->insert([
                    'uid'=>$v['uid'],
                    'wxapp_id'=>$v['wxapp_id'],
                    'pay_uniacid'=>334,
                    'mch_id'=>$v['mch_id'],
                    'giftlog_id'=>$v['id'],
                    'live_id'=>$v['live_id'],
                    'order_amount'=>$v['amount'],
                    'sharing_amount'=>$v['amount']*0.3,
                    'transaction_id'=>$v['transaction_id'],
                    'out_order_no'=>$v['out_trade_no'],
                    'status'=>0,
                    'creat_time'=>time()
                ]);
            
            }
        }
        echo "<script>setTimeout(function(){
	window.location.href='/admin.php/finance/index?page=".($_GET['page']+1)."';
},500);</script>";
        exit();
        $mchA = [5=>'1571615431',12=>'1567597801'];//可赞
        $mchB = [17=>'1609226509',24=>'1609549160'];//B座
        $startDate = '2022-01-01';
        $endDate = '2022-01-19';
        foreach($mchA as $k=>$v)
        {
            $mchA[$v]['sum'] = $this->getFinanceDataByMchid($k,$startDate,$endDate);
        }
        foreach($mchB as $k=>$v)
        {
            $mchB[$v]['sum'] = $this->getFinanceDataByMchid($k,$startDate,$endDate);
        }
        $this->assign([
            'mchA'=>$mchA,
            'mchB'=>$mchB,
        ]);
        return view();*/
    }
    
    
    private function getFinanceDataByMchid($id,$startDate,$endDate)
    {
        $pay_uniacid = $id;
        if($pay_uniacid == 77)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',177)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$id:$id;
        }
        if($pay_uniacid == 37)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$id:$id;
        }
        return Db::connect("live_db_config")->table('ims_wzbagent_giftlogs')->whereTime('dateline', 'between', [$startDate, $endDate]) ->where('status',1)->where('pay_uniacid','IN',$pay_uniacid)->value('sum(amount)')/100;
    }
    
    public function profitSharingMchList()
    {
        /*$mch[0] = [
            
            5=>['mch_id'=>'1571615431','data'=>$this->profitSharingGetMchData(5,0),'pay_uniacid'=>5],
            12=>['mch_id'=>'1567597801','data'=>$this->profitSharingGetMchData(12,0),'pay_uniacid'=>12],
            ];
        $mch[1] = [
                17=>['mch_id'=>'1609226509','data'=>$this->profitSharingGetMchData(17,0),'pay_uniacid'=>17],
                24=>['mch_id'=>'1609549160','data'=>$this->profitSharingGetMchData(24,0),'pay_uniacid'=>24],
                30=>['mch_id'=>'1626951488','data'=>$this->profitSharingGetMchData(30,0),'pay_uniacid'=>30],
                //31=>['mch_id'=>'1627545180','data'=>$this->profitSharingGetMchData(31,0),'pay_uniacid'=>31]
                ];*/
                
        $mch[0] = $mch[1] = 0;
        $mch[2] = [
            // 25=>['mch_id'=>'1620905287','data'=>$this->profitSharingGetMchData(25,0),'pay_uniacid'=>25],
            // 33=>['mch_id'=>'1623269128','data'=>$this->profitSharingGetMchData(33,0),'pay_uniacid'=>33],
            // 77=>['mch_id'=>'1634480723','data'=>$this->profitSharingGetMchData(77,0),'pay_uniacid'=>77],
            // 147=>['mch_id'=>'1635811722','data'=>$this->profitSharingGetMchData(147,0),'pay_uniacid'=>147],
        ];
        $mch[3] = [
            // 35=>['mch_id'=>'1630759818','data'=>$this->profitSharingGetMchData(35,0),'pay_uniacid'=>35],
            // 36=>['mch_id'=>'1632092555','data'=>$this->profitSharingGetMchData(36,0),'pay_uniacid'=>36],
            // 37=>['mch_id'=>'1634435244','data'=>$this->profitSharingGetMchData(37,0),'pay_uniacid'=>37],
            // 314=>['mch_id'=>'1638862926','data'=>$this->profitSharingGetMchData(314,0),'pay_uniacid'=>314],
            // 315=>['mch_id'=>'1639874070','data'=>$this->profitSharingGetMchData(315,0),'pay_uniacid'=>315],
            // 78=>['mch_id'=>'1640270161','data'=>$this->profitSharingGetMchData(78,0),'pay_uniacid'=>78],
            // 334=>['mch_id'=>'1645462842','data'=>$this->profitSharingGetMchData(334,0),'pay_uniacid'=>334],
            // 337=>['mch_id'=>'1646557208','data'=>$this->profitSharingGetMchData(337,0),'pay_uniacid'=>337],
            // 338=>['mch_id'=>'1646557208','data'=>$this->profitSharingGetMchData(338,0),'pay_uniacid'=>338],
        ];
        $this->assign([
            'mch'=>$mch,
        ]);
        return view();
    }

    public function profitSharingMchListNew()
    {
//        new_live_db_config

//        $arr = Db('finance')->select();
//
//        $mch = array();
//        $mch[0] = $mch[1] = 0;
//        foreach($arr as $key => $val){
//            if($val['department'] != '' || $val['department'] != NULL){
//                $mch[$val['department']][$val['pay_uniacid']] = ['mch_id'=>$val['mch_id'],'data'=>$this->profitSharingGetMchData($val['pay_uniacid'],0),'pay_uniacid'=>$val['pay_uniacid']];
//            }
//        }
//
//        $this->assign([
//            'mch'=>$mch,
//        ]);
//        return view();
//------------------------------------------------------------------------------------------------------------------

        $mch[0] = $mch[1] = 0;
        $mch[2] = [
//            25=>['mch_id'=>'1620905287','data'=>$this->profitSharingGetMchDataNew(25,0),'pay_uniacid'=>25],
//            33=>['mch_id'=>'1623269128','data'=>$this->profitSharingGetMchDataNew(33,0),'pay_uniacid'=>33],
//            77=>['mch_id'=>'1634480723','data'=>$this->profitSharingGetMchDataNew(77,0),'pay_uniacid'=>77],
//            147=>['mch_id'=>'1635811722','data'=>$this->profitSharingGetMchDataNew(147,0),'pay_uniacid'=>147],
        ];
        $mch[3] = [
//            35=>['mch_id'=>'1630759818','data'=>$this->profitSharingGetMchDataNew(35,0),'pay_uniacid'=>35],
//            36=>['mch_id'=>'1632092555','data'=>$this->profitSharingGetMchDataNew(36,0),'pay_uniacid'=>36],
//            37=>['mch_id'=>'1634435244','data'=>$this->profitSharingGetMchDataNew(37,0),'pay_uniacid'=>37],
//            314=>['mch_id'=>'1638862926','data'=>$this->profitSharingGetMchDataNew(314,0),'pay_uniacid'=>314],
//            315=>['mch_id'=>'1639874070','data'=>$this->profitSharingGetMchDataNew(315,0),'pay_uniacid'=>315],
//            78=>['mch_id'=>'1640270161','data'=>$this->profitSharingGetMchDataNew(78,0),'pay_uniacid'=>78],

            // 335=>['mch_id'=>'1645462849','data'=>$this->profitSharingGetMchDataNew(335,0),'pay_uniacid'=>335],
            // 336=>['mch_id'=>'1645447933','data'=>$this->profitSharingGetMchDataNew(336,0),'pay_uniacid'=>336],

            // 337=>['mch_id'=>'1646555768','data'=>$this->profitSharingGetMchDataNew(337,0),'pay_uniacid'=>337],
            // 334=>['mch_id'=>'1646558883','data'=>$this->profitSharingGetMchDataNew(334,0),'pay_uniacid'=>334],
            // 342=>['mch_id'=>'1648322623','data'=>$this->profitSharingGetMchDataNew(342,0),'pay_uniacid'=>342],
            // 343=>['mch_id'=>'1648755725','data'=>$this->profitSharingGetMchDataNew(343,0),'pay_uniacid'=>343],
            // 341=>['mch_id'=>'1649444259','data'=>$this->profitSharingGetMchDataNew(341,0),'pay_uniacid'=>341],
            // 340=>['mch_id'=>'1649512544','data'=>$this->profitSharingGetMchDataNew(340,0),'pay_uniacid'=>340],
            
            // 349=>['mch_id'=>'1649918888','data'=>$this->profitSharingGetMchDataNew(349,0),'pay_uniacid'=>349],
            // 348=>['mch_id'=>'1650301095','data'=>$this->profitSharingGetMchDataNew(348,0),'pay_uniacid'=>348],
            // 347=>['mch_id'=>'1650536101','data'=>$this->profitSharingGetMchDataNew(347,0),'pay_uniacid'=>347],
            
            // 351=>['mch_id'=>'1650301095','data'=>$thiss->profitSharingGetMchDataNew(351,0),'pay_uniacid'=>351],
            //1656527643=>['mch_id'=>'1656527643','data'=>$this->profitSharingGetMchDataNew(1656527643,0),'pay_uniacid'=>1656527643],
            //1661962275=>['mch_id'=>'1661962275','data'=>$this->profitSharingGetMchDataNew(1661962275,0),'pay_uniacid'=>1661962275],
//            335=>['mch_id'=>'1646555768','data'=>$this->profitSharingGetMchDataNew(335,0),'pay_uniacid'=>335],
//            335=>['mch_id'=>'1646557208','data'=>$this->profitSharingGetMchDataNew(335,0),'pay_uniacid'=>335],
//            335=>['mch_id'=>'1646558883','data'=>$this->profitSharingGetMchDataNew(335,0),'pay_uniacid'=>335],
        ];

//        echo "<pre>";
//        print_r($mch);
//        die;


        $this->assign([
            'mch'=>$mch,
        ]);
        return view();
    }
    
    public function profitSharingOrderList()
    {
        if(input()['limit'])
        {
            $page = input()['page'];
            $limit = input()['limit'];
            $pay_uniacid = input()['pay_uniacid'];
            $keyword = input()['keyword'];
            $list = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title,w.name as wxapp_name')->join('ims_wzbagent_wxapp w','w.id=s.wxapp_id')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('s.pay_uniacid',$pay_uniacid)->where('l.title|w.name|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0);
            $count = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title,w.name as wxapp_name')->join('ims_wzbagent_wxapp w','w.id=s.wxapp_id')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('s.pay_uniacid',$pay_uniacid)->where('l.title|w.name|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0);
            if(input()['status'] !='' )
            {
                $list = $list->where('s.status',input()['status']);
                $count = $count->where('s.status',input()['status']);
            }
            $list = $list->page($page,$limit)->order('s.id DESC')->select();
            $count = $count->count();
            
            foreach ($list as $k=>$v)
            {
                $list[$k]['order_amount'] = $v['order_amount']/100;
                $list[$k]['sharing_amount'] = $v['sharing_amount']/100;
                $list[$k]['status_text'] = $v['status']?'已分账':'待分账';
                $list[$k]['creat_time'] = date('Y-m-d H:i',$v['creat_time']);
                $list[$k]['finish_time'] = $v['finish_time']?date('Y-m-d H:i',$v['finish_time']):'';
            }
            return ['code'=>0,'count'=>$count,'data'=>$list,'msg'=>''];
        }
        else
        {
            return view();
        }
    }

    public function profitSharingOrderListNew()
    {
        if(input()['limit'])
        {
            $page = input()['page'];
            $limit = input()['limit'];
            $pay_uniacid = input()['pay_uniacid'];
            $keyword = input()['keyword'];
            if($pay_uniacid < 1000)
            {
                $list = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title,w.name as wxapp_name')->join('ims_wzbagent_wxapp w','w.id=s.wxapp_id')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('s.pay_uniacid',$pay_uniacid)->where('l.title|w.name|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0);
                $count = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title,w.name as wxapp_name')->join('ims_wzbagent_wxapp w','w.id=s.wxapp_id')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('l.title|w.name|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0)->where('s.pay_uniacid',$pay_uniacid);
            }
            else
            {
                $list = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('l.title|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0)->where('s.mch_id',$pay_uniacid);
                $count = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('l.title|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0)->where('s.mch_id',$pay_uniacid);;
            } 
            if(input()['status'] !='' )
            {
                $list = $list->where('s.status',input()['status']);
                $count = $count->where('s.status',input()['status']);
            }
            $list = $list->page($page,$limit)->order('s.id DESC')->select();
            $count = $count->count();

            foreach ($list as $k=>$v)
            {
                $list[$k]['order_amount'] = $v['order_amount']/100;
                $list[$k]['sharing_amount'] = $v['sharing_amount']/100;
                $list[$k]['status_text'] = $v['status']?'已分账':'待分账';
                $list[$k]['creat_time'] = date('Y-m-d H:i',$v['creat_time']);
                $list[$k]['finish_time'] = $v['finish_time']?date('Y-m-d H:i',$v['finish_time']):'';
            }
            return ['code'=>0,'count'=>$count,'data'=>$list,'msg'=>''];
        }
        else
        {
            return view();
        }
    }
    
    public function doProfitSharing()
    {

//        echo "<pre>";
//        print_r($_GET);
//        die;

        if( in_array(date('H'),['10,11,20,21']) && input('auto') )
        {
            return json_encode(['code'=>1,'msg'=>'投票高峰期禁止使用']);
        }
        set_time_limit(0);
        $mchData = [
                12=>['openid'=>'otTED47-C_2f_nDjg6jA7IaVVmEE','appid'=>'wx6cdea79ce799ada4','mch_id'=>'1567597801','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],
                5=>['openid'=>'oJNDS5cALO-NAndV3uW1khGu6dNg','appid'=>'wx1308d1e7d62384e8','mch_id'=>'1571615431','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],

                25=>['openid'=>'oNPiu5JTBuuo13BJmDfDuLuR858A','appid'=>'wx9a562258b4fe5546','mch_id'=>'1620905287','mch_secrect'=>'muj4rqngzplib0t21mr41i11iltkhzbv'],
                33=>['openid'=>'ortqL4qAxrtjsDfjrhComfQMlbaQ','appid'=>'wxf793f375e97faa04','mch_id'=>'1623269128','mch_secrect'=>'d4uepvjoant2uxcxbgwekslbn2vf3viu'],

                24=>['openid'=>'ogpbF4pwxjhFlX7WGGSqGwoQY5Mo','appid'=>'wx1116df9b94b6bd93','mch_id'=>'1609549160','mch_secrect'=>'uc0kxqezvhwwmo2fqrvlqnz15dwh04yf'],
                17=>['openid'=>'ogpbF4pwxjhFlX7WGGSqGwoQY5Mo','appid'=>'wx1116df9b94b6bd93','mch_id'=>'1609226509','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],

                30=>['openid'=>'oKM4m5ALgWeHmA1f4dl4xE2pXacI','appid'=>'wx6529a491d9824e20','mch_id'=>'1626951488','mch_secrect'=>'ytahyvputnuzd0acnslilyl0dknowtza'],

                31=>['openid'=>'orEKV5RkAR1SgDj7KoCc4-9iP7cU','appid'=>'wx8f55dc7acef8842d','mch_id'=>'1627545180','mch_secrect'=>'sf4pisvhb3ylcvmbxq3hapopultbrf1d'],

                35=>['openid'=>'oUUEc5e-bac4mUhMNvKt0W9j9x0g','appid'=>'wx853fb3dade28476b','mch_id'=>'1630759818','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                36=>['openid'=>'on5o35HBbFPqueiksiYB2J-HYuCU','appid'=>'wxfedfeb78b441bf26','mch_id'=>'1632092555','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                37=>['openid'=>'oN__T5ZdYQ9u3DDvxfAqIa3Km3pY','appid'=>'wxfa94657fe6521847','mch_id'=>'1634435244','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                77=>['openid'=>'oRXHv5Gdc0_gW47kE9G1ZPKD8isw','appid'=>'wxf1d9a5e4b7441488','mch_id'=>'1634480723','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                34=>['openid'=>'oPHtR6cS2brS3xzzH4Uy1_LUCEiY','appid'=>'wx896cb6302187d842','mch_id'=>'1635738483','mch_secrect'=>'yo5hsqrdfifnvj3py514onyxkpxcmibu'],
                34=>['openid'=>'oPHtR6cS2brS3xzzH4Uy1_LUCEiY','appid'=>'wx896cb6302187d842','mch_id'=>'1635738483','mch_secrect'=>'yo5hsqrdfifnvj3py514onyxkpxcmibu'],
                254=>['openid'=>'o5SPI6HcgF-SXittSxMuErpVy7w8','appid'=>'wx1a363cdd0a2a5658','mch_id'=>'1636300633','mch_secrect'=>'yo5hsqrdfifnvj3py514onyxkpxcmibu'],

                7=>['openid'=>'oLYZF5MQNnwWEPDLm4CISxzC42Vg','appid'=>'wx5b1b61865206bfdc','mch_id'=>'1581054411','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],
                13=>['openid'=>'oQm-k5ekiAX73CK3wrzgBHCzj4bI','appid'=>'wx73d257f00aa4e670','mch_id'=>'1549401141','mch_secrect'=>'buFLzNXxXtxNS3WxFIk3HNVboqLFeOCA'],
                32=>['openid'=>'oq-Aq5bqcNTbKJwgqnNokKCgURZk','appid'=>'wx3f91df156e9cf812','mch_id'=>'1627368099','mch_secrect'=>'tfbganhnc0ltd01vgzmozkqexv5bh5ew'],
                39=>['openid'=>'oG5gN5UaGmU_v3aHWSL7oy3J_MvI','appid'=>'wx29f3327f4ca22ea2','mch_id'=>'1634533446','mch_secrect'=>'1qcynmptaxfaetd41ofealubrzsanx0t'],
                314=>['openid'=>'oebS74iJ7kTdmIMTc7Ptp1qmZcxc','appid'=>'wxdbef2bcdaf0c7432','mch_id'=>'1640270161','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                315=>['openid'=>'oLgez5PhEFx4VwhojbkuW3lCTg4A','appid'=>'wxfadfe1fa3773bdb2','mch_id'=>'1639874070','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                78=>['openid'=>'oIQGY5dC_MQly_x8cob2U-chGurA','appid'=>'wx8d07bc3b28bf64b9','mch_id'=>'1640367186','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                
                // o8kvB5LyT4YzrR2XMOVtJE4sbOmE wxd5af28c829a7ec95
                334=>['openid'=>'oSQgn45sOZHpMQlo-VGnG-0mMKm8','appid'=>'wx42c706b0508fa3d5','mch_id'=>'1645462842','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                //334=>['openid'=>'o8kvB5LyT4YzrR2XMOVtJE4sbOmE','appid'=>'wxd5af28c829a7ec95','mch_id'=>'1645462842','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                337=>['openid'=>'oSQgn4zrNFXFn0GwPLOx3qFXHVPA','appid'=>'wx42c706b0508fa3d5','mch_id'=>'1646557208','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                338=>['openid'=>'o8kvB5LyT4YzrR2XMOVtJE4sbOmE','appid'=>'wxd5af28c829a7ec95','mch_id'=>'1646557208','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            ];
        $pay_uniacid = $payUniacid = input()['pay_uniacid'];
        if($pay_uniacid == 315)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','=',315)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 314)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','=',314)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 78)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','=',78)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 77)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',147)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 147)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',146)->where('uniacid','<',247)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 37)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 39)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',204)->where('uniacid','>',40)->where('uniacid','<',115)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 34)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',204)->where('uniacid','>',152)->where('uniacid','<',352)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 254)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',204)->where('uniacid','>',253)->where('uniacid','<',454)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }

        $list = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('pay_uniacid','IN',$pay_uniacid)->where('status',0)->order('id DESC')->limit(0,200)->select();
        //var_dump($list);exit;
        $time = time();
        $successCount = 0;
        $totalCount = count($list);
        //var_dump($list);exit;
        foreach ($list as $k=>$v)
        {
            $mch_id = $mchData[$payUniacid]['mch_id'];
            $mch_secrect = $mchData[$payUniacid]['mch_secrect'];
            $appid = $mchData[$payUniacid]['appid'];
            $openid = $mchData[$payUniacid]['openid'];
            $transaction_id = $v['transaction_id'];
            $out_order_no = $v['out_order_no'];
            $amount = $v['sharing_amount'];
            /*if($amount == 0){
                //echo json_encode(['code'=>0,'successCount'=>0,'totalCount'=>0,'msg'=>'分账金额不合法']);
                continue;
            }*/
            if($pay_uniacid == 310)
                $result = controller('admin/Weixin9782')->fwsProfitsharing($mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);
            else
                $result = controller('admin/Weixin9782')->profitsharing($mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);
            //var_dump($amount);exit;
            //var_dump($result);exit;
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS')
            {
                $sqlStatus = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('id',$v['id'])->update(['status'=>1,'order_id'=>$result['order_id'],'finish_time'=>$time]);
                if($sqlStatus)
                    $successCount ++;
            }
            else if($result['result_code'] == 'FAIL' && $result['return_code'] == 'SUCCESS')
            {
                $sqlStatus = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('id',$v['id'])->update(['status'=>-1,'err_code'=>$result['err_code'],'err_code_des'=>$result['err_code_des']]);
            }
            //var_dump($result,$mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);exit;
        }
        //print_r(['code'=>$code,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        echo json_encode(['code'=>1,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        
    }

    public function doProfitSharingNew()
    {
        if( in_array(date('H'),['10,11,20,21']) && input('auto') )
        {
            return json_encode(['code'=>1,'msg'=>'投票高峰期禁止使用']);
        }
        set_time_limit(0);
        $mchData = [
            78=>['openid'=>'oIQGY5dC_MQly_x8cob2U-chGurA','appid'=>'wx8d07bc3b28bf64b9','mch_id'=>'1640367186','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
//            335=>['openid'=>'oSQgn45sOZHpMQlo-VGnG-0mMKm8','appid'=>'wx2f6dfd661cb9e3e9','mch_id'=>'1645462849','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            335=>['openid'=>'oSQgn4zrNFXFn0GwPLOx3qFXHVPA','appid'=>'wx42c706b0508fa3d5','mch_id'=>'1645462849','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
//            335=>['openid'=>'o4RQb5GR_dWA6ibdVotiB4CT8Jyc','appid'=>'wx2f6dfd661cb9e3e9','mch_id'=>'1645462849','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],

//            336=>['openid'=>'oSQgn4zrNFXFn0GwPLOx3qFXHVPA','appid'=>'wx039cb39172489bb8','mch_id'=>'1645447933','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            336=>['openid'=>'o-tGn5eC9b_wPIjYh0-HVVyLSrRM','appid'=>'wx039cb39172489bb8','mch_id'=>'1645447933','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],

//            337=>['openid'=>'o99Gn5RvfrmWhVh1JWiIxxL57NGo','appid'=>'wx2f6dfd661cb9e3e9','mch_id'=>'1646555768','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            337=>['openid'=>'ou8mz5NGd5Umx6946MksEr38p0Eo','appid'=>'wx966bbc33cb1f411b','mch_id'=>'1646555768','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            334=>['openid'=>'oaq4I49qWKjF07dK0AL2FbWsdUfM','appid'=>'wxa66a26c9bfb3d990','mch_id'=>'1646558883','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            342=>['openid'=>'oonkD5uGLJtPdi4GqPnTpjJy7b3g','appid'=>'wx409dcff6f97f7a50','mch_id'=>'1648322623','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            //343=>['openid'=>'oOrwS5ld2kOqhhS2qh0lqqXio37w','appid'=>'wx516069ccea88016f','mch_id'=>'1648755725','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            343=>['openid'=>'oYCGC6wOMBT-V4II4eZlUAmvCYiU','appid'=>'wxb9e0de7cf151372c','mch_id'=>'1648755725','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            //341=>['openid'=>'opmCD69JJFU1vTgvqOqULedCKF0U','appid'=>'wxc3618efa882d2384','mch_id'=>'1649444259','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            341=>['openid'=>'oy-md4sksmiIgP9LaNvmSjJ63ZzY','appid'=>'wx124c88f2f99d5d92','mch_id'=>'1649444259','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            340=>['openid'=>'o0X2E6yN1_thwrq0MTVN39ioe8tk','appid'=>'wxad7af1a89c8788ca','mch_id'=>'1649512544','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            349=>['openid'=>'o5mVY5IbdTusKiWF_3iJ-7Z7rFcw','appid'=>'wxb72cdf7252d7b2f9','mch_id'=>'1649918888','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            348=>['openid'=>'oHM6B67BooVAIOOIYIAmxSRyF4H8','appid'=>'wxb2cc804deba9029d','mch_id'=>'1650301095','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            347=>['openid'=>'oNVuR64nwMwxWzxdaX6VacRS_JYo','appid'=>'wxc2dd82c0b01cd237','mch_id'=>'1650536101','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            351=>['openid'=>'oHM6B67BooVAIOOIYIAmxSRyF4H8','appid'=>'wxb2cc804deba9029d','mch_id'=>'1650301095','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            1656527643=>['openid'=>'oEYnp6MleGC4bEET3_mNso9HlBxQ','appid'=>'wxacbd424fb9d423e3','mch_id'=>'1656527643','mch_secrect'=>'5khhz4144ohvvjqld0wqptbtw2cvmojx'],
            1661962275=>['openid'=>'or8sa6nM7wvKnuB442-5ONSMm0lE','appid'=>'wx641440d6edbc57ab','mch_id'=>'1661962275','mch_secrect'=>'5khhz4144ohvvjqld0wqptbtw2cvmojx'],
        ];
        $pay_uniacid = $payUniacid = input()['pay_uniacid'];
        
        if($pay_uniacid < 1000)
            $list = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->where('pay_uniacid','IN',$pay_uniacid)->where('status',0)->order('id desc')->limit(0,200)->select();
        else
            $list = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->where('mch_id',$pay_uniacid)->where('status',0)->order('id desc')->limit(0,200)->select();
        //var_dump($list);exit;
        $time = time();
        $successCount = 0;
        $totalCount = count($list);
        //var_dump($list);exit;
        foreach ($list as $k=>$v)
        {
            $mch_id = $mchData[$payUniacid]['mch_id'];
            $mch_secrect = $mchData[$payUniacid]['mch_secrect'];
            $appid = $mchData[$payUniacid]['appid'];
            $openid = $mchData[$payUniacid]['openid'];
            $transaction_id = $v['transaction_id'];
            $out_order_no = $v['out_order_no'];
            $amount = $v['sharing_amount'];
            /*if($amount == 0){
                //echo json_encode(['code'=>0,'successCount'=>0,'totalCount'=>0,'msg'=>'分账金额不合法']);
                continue;
            }*/
            if($pay_uniacid == 310)
                $result = controller('admin/Weixin9782')->fwsProfitsharing($mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);
            else
                $result = controller('admin/Weixin9782')->profitsharing($mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);
            //var_dump($amount);exit;
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS')
            {
                $sqlStatus = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->where('id',$v['id'])->update(['status'=>1,'order_id'=>$result['order_id'],'finish_time'=>$time]);
                if($sqlStatus)
                    $successCount ++;
            }
            else if($result['result_code'] == 'FAIL' && $result['return_code'] == 'SUCCESS')
            {
                $sqlStatus = Db::connect("new_live_db_config")->table('ims_wzbagent_profit_sharing')->where('id',$v['id'])->update(['status'=>-1,'err_code'=>$result['err_code'],'err_code_des'=>$result['err_code_des']]);
            }
            //var_dump($result,$mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);exit;
        }
        //print_r(['code'=>$code,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        echo json_encode(['code'=>1,'successCount'=>$successCount,'totalCount'=>$totalCount]);

    }
    
    public function doProfitSharingByOpenid()
    {
        set_time_limit(0);
        $open_id = 'o3_515PHCG2E6JZc5V_Oqh6A8qcs';
        $list = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('openid', $open_id)->where('status',0)->order('id asc')->limit(0,60)->select();
        //$mch_id = '1640270161';
        //$list = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('mch_id', $mch_id)->where('status',0)->order('id asc')->limit(0,300)->select();
        //var_dump($list);exit;
        $time = time();
        $successCount = 0;
        $totalCount = count($list);
        //var_dump($list);exit;
        foreach ($list as $k=>$v)
        {
            $mch_id = '1639874070';
            $mch_secrect = 'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY';
            $appid = Db::connect("live_db_config")->table('ims_wzbagent_wxapp')->where('id', $v['wxapp_id'])->value('appid');
            //$openid = $mchData[$payUniacid]['openid'];
            $transaction_id = $v['transaction_id'];
            $out_order_no = $v['out_order_no'];
            $result = controller('admin/Weixin9782')->unfreeze($mch_id,$appid,$transaction_id,$out_order_no,$mch_secrect);
            //var_dump($amount);exit;
            //var_dump($result, $v);exit;
            var_dump($result);
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS')
            {
                $sqlStatus = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('id',$v['id'])->where('status',0)->update(['status'=>2,'order_id'=>$result['order_id'],'finish_time'=>$time, 'mch_id'=>$result['mch_id'],'appid'=>$appid]);
                if($sqlStatus)
                    $successCount ++;
            }
            //var_dump($result,$mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);exit;
        }
        //print_r(['code'=>$code,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        echo json_encode(['code'=>1,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        
    }

    public function profitSharingGetMchData($pay_uniacid,$type = 1)
    {
        if($pay_uniacid)
        {
            $payUniacid = $pay_uniacid;
            if($pay_uniacid == 77)
            {
                $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',147)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            if($pay_uniacid == 147)
            {
                $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',147)->where('uniacid','<',247)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            if($pay_uniacid == 37)
            {
                $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            $unProfitSharing = Db::connect("live_db_config")->field('sum(order_amount) as a1, sum(sharing_amount) as a2, count(*) as c')->table('ims_wzbagent_profit_sharing')->where('status',0)->where('pay_uniacid','IN',$pay_uniacid)->find();
            if($type == 0)
                return $unProfitSharing;
                
            $profitSharing = Db::connect("live_db_config")->field('sum(order_amount) as a1, sum(sharing_amount) as a2, count(*) as c')->table('ims_wzbagent_profit_sharing')->where('status',1)->where('pay_uniacid','IN',$pay_uniacid)->find();
            return (['code'=>1,'unProfitSharing'=>$unProfitSharing,'profitSharing'=>$profitSharing]);
        }
    }

    public function profitSharingGetMchDataNew($pay_uniacid,$type = 1)
    {
        if($pay_uniacid)
        {
            $payUniacid = $pay_uniacid;
            if($pay_uniacid == 77)
            {
                $pay_uniacid = Db::connect("new_live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',147)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            if($pay_uniacid == 147)
            {
                $pay_uniacid = Db::connect("new_live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',147)->where('uniacid','<',247)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            if($pay_uniacid == 37)
            {
                $pay_uniacid = Db::connect("new_live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }

            $unProfitSharing = Db::connect("new_live_db_config")->field('sum(order_amount) as a1, sum(sharing_amount) as a2, count(*) as c')->table('ims_wzbagent_profit_sharing')->where('status',0);
            $unProfitSharing = ($pay_uniacid < 10000)?$unProfitSharing->where('pay_uniacid','IN',$pay_uniacid):$unProfitSharing->where('mch_id',$pay_uniacid);
            $unProfitSharing = $unProfitSharing->find();
            if($type == 0)
            {
                return $unProfitSharing;
            }
                

            $profitSharing = Db::connect("new_live_db_config")->field('sum(order_amount) as a1, sum(sharing_amount) as a2, count(*) as c')->table('ims_wzbagent_profit_sharing')->where('status',1);

            $profitSharing = ($pay_uniacid < 10000)?$profitSharing->where('pay_uniacid','IN',$pay_uniacid):$profitSharing->where('mch_id',$pay_uniacid);

            $profitSharing = $profitSharing->find();
            return (['code'=>1,'unProfitSharing'=>$unProfitSharing,'profitSharing'=>$profitSharing]);
        }
    }

    /**
     * @author  ShuAnG
     * @createDate  2023-03-07
     * 获取分账 账号 列表
     * @return array
     */
    public function add(){
        if(request()->isAjax()){
            $data=input('post.')['data'];

            $is_set = db('finance')->where('',$data['mch_id'])->find();

            if($is_set){
                return ['msg'=>'mch_id重复','icon'=>2];
            }else{
                $add=db('finance')->insert($data);
                if($add){
                    return ['msg'=>'新增分账成功','icon'=>1];
                }else{
                    return ['msg'=>'新增分账失败','icon'=>2];
                }
            }

        }
        return view();
    }


    /**
     * @author  ShuAnG
     * @createDate  2023-06-06
     * 获取分账 账号 列表
     * @return array|void
     */
    public function edit(){
        if(request()->isAjax()){
            $data=input('post.')['data'];
            $save=db('finance')->where('mch_id',$data['mch_id'])->update($data);
            if($save !== false){
                return ['msg'=>'编辑分账成功','icon'=>1];
            }else{
                return ['msg'=>'编辑分账失败','icon'=>2];
            }
            return;
        }
        $finance=db('finance')->where('mch_id',input('mch_id'))->find();
        $this->assign('finance',$finance);
        return view();
    }
    
    
}